package com.gxa.p2p.service.impl;

import com.gxa.p2p.mapper.BorrowuserMapper;
import com.gxa.p2p.pojo.Borrowuser;
import com.gxa.p2p.service.BorrowuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Transactional(rollbackFor = {Throwable.class})
@Service("borrowuserService")
public class BorrowuserServiceImpl implements BorrowuserService {
    @Autowired
    BorrowuserMapper borrowuserMapper;
    @Override
    public Borrowuser selectById(Long id) {
        return borrowuserMapper.selectById(id);
    }

    @Override
    public Boolean add(Borrowuser borrowuser) {
        borrowuser.setRegTime(new Timestamp(System.currentTimeMillis()));
        return borrowuserMapper.add(borrowuser);
    }

    @Override
    public void updatePwd(Long id, String loginPwd, String dealPwd) {
         borrowuserMapper.updatePwd(id, loginPwd, dealPwd);
    }

    @Override
    public List<Map<String, Object>> findByName(String companyName) {
        if (companyName !=null && companyName !=""){
            companyName = "%"+companyName+"%";
        }
        return borrowuserMapper.findByName( companyName,null);
    }

    @Override
    public List<Map<String, Object>> findByPage(String companyName, String phone, Boolean lockState, Integer page, Integer size) {
        if (companyName !=null && companyName !=""){
            companyName = "%"+companyName+"%";
        }
        if (phone !=null && phone !=""){
            phone = "%"+phone+"%";
        }
        return borrowuserMapper.findByPage(companyName, phone, lockState, (page-1)*size, size);
    }

    @Override
    public List<Map<String, Object>> count(String companyName, String phone, Boolean lockState, Integer page, Integer size) {
        if (companyName !=null && companyName !=""){
            companyName = "%"+companyName+"%";
        }
        if (phone !=null && phone !=""){
            phone = "%"+phone+"%";
        }
        return borrowuserMapper.findByPage(companyName, phone, lockState, null, null);
    }

    @Override
    public void updateBorrowuser(Borrowuser borrowuser) {
         borrowuserMapper.updateBorrowuser(borrowuser);
    }
}
